﻿using System.Collections.Generic;
using System.Linq;
using System.Windows;
using WorkoutDictionary.Model;
using WorkoutDictionary.Model.Tables;
using System;
using WorkoutDictionary.ViewModel;

namespace WorkoutDictionary.Managers
{
    public class StorageManager
    {
        private WorkoutDictionaryDataContext database;
        private WorkoutDictionaryDataContext Database
        {
            get
            {
                if (this.database == null)
                {
                    this.database = ((App)Application.Current).WorkoutDictionaryDataContext;
                }
                return database;
            }
        }

        public List<string> GetAllTypes()
        {
            var typesTable = this.Database.GetTable<WorkoutType>();
            if(typesTable.Any())
            {
                return typesTable.Select(wt => wt.Name).ToList();
            }
            return new List<string>();
        }

        public int GetTypesCount()
        {
            return this.Database.GetTable<WorkoutType>().Count();
        }

        public Guid AddWorkoutType(string name)
        {
            var typesTable = this.Database.GetTable<WorkoutType>();
            if (typesTable.All(t => t.Name != name))
            {
                var type = new WorkoutType
                               {
                                   ID = Guid.NewGuid(),
                                   Name = name
                               };
                this.Database.GetTable<WorkoutType>().InsertOnSubmit(type);
                this.Database.SubmitChanges();
                return type.ID;
            }
            return typesTable.First(t => t.Name == name).ID;
        }

        public void DeleteWorkoutType(string name)
        {
            var typesTable = this.Database.GetTable<WorkoutType>();
            var type = typesTable.FirstOrDefault(t => t.Name == name);
            if (type != null)
            {
                typesTable.DeleteOnSubmit(type);
                this.Database.SubmitChanges();
            }
        }

        public Guid CreateAnExercise(string name, string weights, int status, Guid workoutID)
        {
            var id = Guid.NewGuid();
            var exercise = new Exercise
                               {
                                   ID = id,
                                   Name = name,
                                   Weights = weights,
                                   Status = status,
                                   WorkoutID = workoutID
                               };
            this.Database.GetTable<Exercise>().InsertOnSubmit(exercise);
            this.Database.SubmitChanges();

            return id;
        }

        public List<Exercise> GetExercises(Guid workoutID)
        {
            return this.Database.GetTable<Exercise>().Where(ex => ex.WorkoutID == workoutID).ToList();
        }

        public void CreateWorkout(Guid workoutID, string type)
        {
            Guid typeID;
            var t = this.Database.GetTable<WorkoutType>().FirstOrDefault(tt => tt.Name == type);
            typeID = t != null ? t.ID : this.AddWorkoutType(type);
            var workout = new Workout
                              {
                                  ID = workoutID,
                                  Date = DateTime.Now,
                                  WorkoutTypeID = typeID
                              };
            this.Database.GetTable<Workout>().InsertOnSubmit(workout);
            this.Database.SubmitChanges();
        }

        public List<WorkoutViewModel> GetWorkoutsViewModels()
        {
            var typesTable = this.Database.GetTable<WorkoutType>();
            return this.Database.GetTable<Workout>()
                .Select
                (
                    w => new WorkoutViewModel
                             {
                                 Date = w.Date,
                                 Type = typesTable.FirstOrDefault(t => t.ID == w.WorkoutTypeID).Name
                             }
                ).ToList();
        }
    }
}